package cinescope2014.boundaries;

import cinescope2014.utilities.Globale;
import java.sql.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author pascal
 */
public class JIFRubriqueCRUD extends javax.swing.JInternalFrame {
    
    private Connection icn;
    private DefaultTableModel idtm;

    /**
     * Creates new form JIFRubriqueCRUD
     */
    public JIFRubriqueCRUD() {
        initComponents();
        
        setTitle("CRUD Rubrique");
        
        try {
            // --- Connexion
//            Class.forName("org.gjt.mm.mysql.Driver");
//            String lsDSN = "jdbc:mysql://127.0.0.1:3306/cinescope2014";
//            icn = DriverManager.getConnection(lsDSN, "root", "");

            icn = Globale.getConnexion();
            idtm = (DefaultTableModel) jTableRubriques.getModel();
            
            String lsSQL;
            PreparedStatement lpst;
            ResultSet lrs;
            
            lsSQL = "SELECT * FROM rubrique ORDER BY code_rubrique";
            lpst = icn.prepareStatement(lsSQL);
            lrs = lpst.executeQuery();
            
            Object[] tLigne;
            
            while (lrs.next()) {
                tLigne = new Object[3];
                
                tLigne[0] = lrs.getString(1);
                tLigne[1] = lrs.getString(2);
                tLigne[2] = lrs.getString(3);
                
                idtm.addRow(tLigne);
            }
            
            lrs.close();
            lpst.close();
            
            jLabelMessage.setText("Okay !!!");
            
        } catch (SQLException ex) {
            jLabelMessage.setText(ex.getMessage());
        }
        
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRubriques = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelMessage = new javax.swing.JLabel();
        jTextFieldCode = new javax.swing.JTextField();
        jTextFieldNom = new javax.swing.JTextField();
        jButtonAjouter = new javax.swing.JButton();
        jButtonSupprimer = new javax.swing.JButton();
        jButtonModifier = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabelID = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jTableRubriques.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Code", "Nom"
            }
        ));
        jTableRubriques.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableRubriquesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableRubriques);

        jLabel1.setText("Code");

        jLabel2.setText("Nom");

        jLabelMessage.setText("Message");

        jTextFieldCode.setText("NF");

        jTextFieldNom.setText("Nouveaux films");

        jButtonAjouter.setText("Ajouter");
        jButtonAjouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouterActionPerformed(evt);
            }
        });

        jButtonSupprimer.setText("Supprimer");
        jButtonSupprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSupprimerActionPerformed(evt);
            }
        });

        jButtonModifier.setText("Modifier");
        jButtonModifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModifierActionPerformed(evt);
            }
        });

        jLabel5.setText("ID");

        jLabelID.setForeground(new java.awt.Color(255, 0, 51));
        jLabelID.setText("ID");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jLabelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldCode, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelID, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAjouter)
                        .addGap(42, 42, 42)
                        .addComponent(jButtonModifier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonSupprimer)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabelID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldCode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAjouter)
                    .addComponent(jButtonModifier)
                    .addComponent(jButtonSupprimer))
                .addGap(18, 18, 18)
                .addComponent(jLabelMessage)
                .addGap(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // 

//        try {
//            icn.close();
//        } catch (SQLException e) {
//            System.err.println(e.getMessage());
//        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void jButtonAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouterActionPerformed
        // 
        // AJOUTER CONTROLE SAISIES
        try {
            String lsSQL;
            lsSQL = "INSERT INTO rubrique(code_rubrique, intitule_rubrique) VALUES(?,?)";
            PreparedStatement lpst = icn.prepareStatement(lsSQL);
            
            lpst.setString(1, jTextFieldCode.getText());
            lpst.setString(2, jTextFieldNom.getText());
            lpst.executeUpdate();
            icn.commit();
            lpst.close();
            String[] tLigne = new String[3];
            tLigne[0] = "";
            tLigne[1] = jTextFieldCode.getText();
            tLigne[2] = jTextFieldNom.getText();
            idtm.addRow(tLigne);
            jLabelMessage.setText("Okay !!!");
        } catch (SQLException e) {
            jLabelMessage.setText(e.getMessage());
            System.err.println(e.getMessage());
        }
    }//GEN-LAST:event_jButtonAjouterActionPerformed

    private void jButtonModifierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifierActionPerformed
        // 
        try {
            String lsSQL;
            lsSQL = "UPDATE rubrique SET code_rubrique=?, intitule_rubrique=? WHERE id_rubrique=?";
            PreparedStatement lpst = icn.prepareStatement(lsSQL);
            
            lpst.setString(1, jTextFieldCode.getText());
            lpst.setString(2, jTextFieldNom.getText());
            lpst.setString(3, jLabelID.getText());
            lpst.executeUpdate();
            icn.commit();
            lpst.close();
            int liLigne = jTableRubriques.getSelectedRow();
            jTableRubriques.setValueAt(jTextFieldCode.getText(), liLigne, 1);
            jTableRubriques.setValueAt(jTextFieldNom.getText(), liLigne, 2);
            jLabelMessage.setText("Okay !!!");
        } catch (SQLException e) {
            jLabelMessage.setText(e.getMessage());
            System.err.println(e.getMessage());
        }
    }//GEN-LAST:event_jButtonModifierActionPerformed

    private void jButtonSupprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSupprimerActionPerformed
        // 
        if (jTableRubriques.getSelectedRow() != -1) {
            try {
                String lsSQL;
                lsSQL = "DELETE FROM rubrique WHERE id_rubrique=?";
                PreparedStatement lpst = icn.prepareStatement(lsSQL);
                
                lpst.setString(1, jLabelID.getText());
                lpst.executeUpdate();
                icn.commit();
                lpst.close();
                idtm.removeRow(jTableRubriques.getSelectedRow());
                jLabelMessage.setText("Okay !!!");
            } catch (SQLException e) {
                jLabelMessage.setText(e.getMessage());
                System.err.println(e.getMessage());
            }
        } else {
            jLabelMessage.setText("Sélectionnez une ligne !!!");
        }
    }//GEN-LAST:event_jButtonSupprimerActionPerformed

    private void jTableRubriquesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableRubriquesMouseClicked
        // 
        int liLigne = jTableRubriques.getSelectedRow();
        
        String lsID = jTableRubriques.getValueAt(liLigne, 0).toString();
        String lsCode = jTableRubriques.getValueAt(liLigne, 1).toString();
        String lsNom = jTableRubriques.getValueAt(liLigne, 2).toString();
        
        jLabelID.setText(lsID);
        jTextFieldCode.setText(lsCode);
        jTextFieldNom.setText(lsNom);
    }//GEN-LAST:event_jTableRubriquesMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAjouter;
    private javax.swing.JButton jButtonModifier;
    private javax.swing.JButton jButtonSupprimer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelID;
    private javax.swing.JLabel jLabelMessage;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableRubriques;
    private javax.swing.JTextField jTextFieldCode;
    private javax.swing.JTextField jTextFieldNom;
    // End of variables declaration//GEN-END:variables
}
